package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
/**
 * www.voc.com.cn 华声论坛
 * @author KK
 */
public class VocBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://cs.voc.com.cn/forum.php\\?mod=viewthread&tid=[0-9]*.*",
				"http://cs.voc.com.cn/thread-[0-9]*-1-1.html",
				"http://bbs.voc.com.cn/topic-[0-9]*-1-1.html",
				//http://bbs.voc.com.cn/topic-5952329-1-1.html
				"http://bbs.voc.com.cn/viewthread.php\\?.*tid=[0-9]*.*",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = url;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("title").text();
		if (StringUtils.isNotEmpty(title)){
			title = StringUtils.substringBeforeLast(title, " - ");
			title = StringUtils.substringBeforeLast(title, " - ");
		}



		boolean match = false;
		if (!match){
			Elements select = doc.select("div#content_id div[id^=post_]");

			if (select.size()>0){
				Element div1  = select.get(0);
				match = true;

				String text = doc.select("div#content_id div.spaceborder div.right").text();
				String regex = ".*共([0-9]*)个阅读者,([0-9]*)条回复.*";
				if (text.matches(regex)){
					clickNum = RegexUtils.getMatchGroupString(text, regex, 1);
					commnetNum = RegexUtils.getMatchGroupString(text, regex, 2);
				}

				dateStr = div1.select("div[id^=message] font[color=Silver]").text();
				if (StringUtils.isNullOrEmpty(dateStr)){
					dateStr = div1.select("div#content-info").html();
				}
				content = HtmlCleaner.getContentHtml(url,  div1.select("div[id^=message]"));
				author = div1.select("div.authorForTuGeLeZi").attr("anthorname");
			}
		}
		if (!match){
			Elements select = doc.select("div#postlist > div[id^=post_]");
			if (select.size()>0){
				match = true;
				Element div1 = select.get(0);
				String text = doc.select("div#wp div#ct div.comiis_vbg").text();
				String regex = ".*阅读[(]([0-9]*)[)]  回复[(]([0-9]*)[)].*";
				if (text.matches(regex)){
					clickNum = RegexUtils.getMatchGroupString(text, regex, 1);
					commnetNum = RegexUtils.getMatchGroupString(text, regex, 2);
				}
				dateStr = div1.select("div.authi em span").attr("title");
				if (StringUtils.isNullOrEmpty(dateStr)){
					dateStr = div1.select("div.authi em").text();

				}
				author = div1.select("div.authorForTuGeLeZi").attr("anthorname");
				content = HtmlCleaner.getContentHtml(url, div1.select("div.pcb"));
			}
		}

		if (!match){
			Elements select = doc.select("body > center > div.spaceborder");
			if (select.size()>1){
				match = true;
				String text = select.get(0).text();
				String regex = ".*共([0-9]*)个阅读者,([0-9]*)条回复.*";
				if (text.matches(regex)){
					clickNum = RegexUtils.getMatchGroupString(text, regex, 1);
					commnetNum = RegexUtils.getMatchGroupString(text, regex, 2);
				}

				Element div1 = select.get(1);
				dateStr = div1.select("div#content-info").html();
				content = HtmlCleaner.getContentHtml(url,  div1.select("div[id^=message]"));
				author = div1.select("div.authorForTuGeLeZi").attr("anthorname");
			}
		}


		date = DateUtils.matchDate(dateStr);

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(4);
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}



	


	
}
